package com.wephone.Student.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.data.CityData;
import com.wephone.Student.bean.data.CountryData;
import com.wephone.Student.bean.data.ProvinceData;
import com.wephone.Student.bean.savedData.MutualSaveData3;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.wheel.widget.OnWheelChangedListener;
import com.wephone.Student.wheel.widget.StringWheelAdapter;
import com.wephone.Student.wheel.widget.WheelView;

public class CreateMutualTransaction3Activity extends BaseActivity {
	private EditText inputEdit1;
	private EditText inputEdit2;
	private TextView inputEdit3;
	private EditText inputEdit4;
	private EditText inputEdit5;
	private EditText inputEdit9;
	private RadioButton jobTypeRadio1;
	private RadioButton jobTypeRadio2;
	private RadioButton jobExpRadio1;
	private RadioButton jobExpRadio2;
	private static MutualSaveData3 mutualSaveData3 = new MutualSaveData3();
	private boolean notSave = false;
	private ProgressDialog progressDialog;
	private int nCountryIdx;
	private int nProvinceIdx;
	private int nCityIdx;

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(CreateMutualTransaction3Activity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				if(getDataSuccess){
//					FlyTogetherListResponse res = (FlyTogetherListResponse)response;
					notSave = true;
					setResult(222);
					finish();
				}
			}
			
			@Override
			public void onFailure(Throwable arg0, String result) {
				super.onFailure(arg0, result);
				
				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				Toast.makeText(getApplicationContext(), getString(R.string.network_error_txt), Toast.LENGTH_SHORT).show();
			}
		};
		return handler;
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		inputEdit1.setText(mutualSaveData3.getInputStr1());
		inputEdit2.setText(mutualSaveData3.getInputStr2());
		inputEdit3.setText(mutualSaveData3.getInputStr3());
		inputEdit4.setText(mutualSaveData3.getInputStr4());
		inputEdit5.setText(mutualSaveData3.getInputStr5());
		inputEdit9.setText(mutualSaveData3.getInputStr6());
		if(mutualSaveData3.getRadioIndx1()==0){
			jobTypeRadio1.setChecked(true);
		}else{
			jobTypeRadio2.setChecked(true);
		}
		if(mutualSaveData3.getRadioIndx2()==0){
			jobExpRadio1.setChecked(true);
		}else{
			jobExpRadio2.setChecked(true);
		}
		nCountryIdx = mutualSaveData3.getnCountryIdx();
		nProvinceIdx = mutualSaveData3.getnProvinceIdx();
		nCityIdx = mutualSaveData3.getnCityIdx();
	}

	@Override
	protected void onPause() {
		super.onPause();
		
		if(!notSave){
			mutualSaveData3.setInputStr1(inputEdit1.getText().toString());
			mutualSaveData3.setInputStr2(inputEdit2.getText().toString());
			mutualSaveData3.setInputStr3(inputEdit3.getText().toString());
			mutualSaveData3.setInputStr4(inputEdit4.getText().toString());
			mutualSaveData3.setInputStr5(inputEdit5.getText().toString());
			mutualSaveData3.setInputStr6(inputEdit9.getText().toString());
			if(jobTypeRadio1.isChecked()){
				mutualSaveData3.setRadioIndx1(0);
			}else{
				mutualSaveData3.setRadioIndx1(2);
			}
			if(jobExpRadio1.isChecked()){
				mutualSaveData3.setRadioIndx2(0);
			}else{
				mutualSaveData3.setRadioIndx2(1);
			}
			mutualSaveData3.setnCountryIdx(nCountryIdx);
			mutualSaveData3.setnProvinceIdx(nProvinceIdx);
			mutualSaveData3.setnCityIdx(nCityIdx);
		}else{
			mutualSaveData3 = new MutualSaveData3();
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_create_mutual_transaction_3);
		
		init();
	}
	
	private void init(){
		inputEdit1 = (EditText)findViewById(R.id.recruit_input_edit_1);
		inputEdit2 = (EditText)findViewById(R.id.recruit_input_edit_2);
		inputEdit3 = (TextView)findViewById(R.id.recruit_input_edit_3);
		inputEdit4 = (EditText)findViewById(R.id.recruit_input_edit_4);
		inputEdit5 = (EditText)findViewById(R.id.recruit_input_edit_5);
		inputEdit9 = (EditText)findViewById(R.id.recruit_input_edit_9);
		jobTypeRadio1 = (RadioButton)findViewById(R.id.job_type_radio_1);
		jobTypeRadio2 = (RadioButton)findViewById(R.id.job_type_radio_2);
		jobExpRadio1 = (RadioButton)findViewById(R.id.job_exp_radio_1);
		jobExpRadio2 = (RadioButton)findViewById(R.id.job_exp_radio_2);
		
		inputEdit3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showChooseCountryDialog5(getString(R.string.create_mutual_transaction_3_item_3));
			}
		});
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		findViewById(R.id.btn_finish).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(checkInput()){
					progressDialog = ProgressDialog.show(CreateMutualTransaction3Activity.this, "提示", "正在提交数据...", true, true);
					
					RequestParams params = new RequestParams();
					params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					params.put("rTitle", inputEdit1.getText().toString());
					params.put("rDetail", inputEdit2.getText().toString());
					params.put("rLoc", inputEdit3.getText().toString());
					if(jobTypeRadio1.isChecked()){
						params.put("rJobType", "0");
					}else{
						params.put("rJobType", "1");
					}
					if(jobExpRadio1.isChecked()){
						params.put("rExpRequirement", "1");
					}else{
						params.put("rExpRequirement", "0");
					}
					params.put("rPrice", inputEdit9.getText().toString());
//					if(!TextUtils.isEmpty(inputEdit4.getText().toString())){
						params.put("rTel", inputEdit4.getText().toString());
//					}else{
//						params.put("rTel", "0");
//					}
					params.put("rPassportRequirement", inputEdit5.getText().toString());
					params.put("rTs", ""+System.currentTimeMillis()/1000);
					HttpClient.getInstance().post(Constants.API_ADD_RECRUIT_INFO_URL, params, getHandler());
				}
			}
		});
		
	}
	
	private boolean checkInput(){
		if(TextUtils.isEmpty(inputEdit1.getText().toString().trim().replace("\n", ""))){
			Toast.makeText(getApplicationContext(), "标题必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}else if(TextUtils.isEmpty(inputEdit2.getText().toString().trim().replace("\n", ""))){
			Toast.makeText(getApplicationContext(), "详情必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}else if(TextUtils.isEmpty(inputEdit3.getText().toString())){
			Toast.makeText(getApplicationContext(), "工作地点必须填写", Toast.LENGTH_SHORT).show();
			return false;
//		}else if(TextUtils.isEmpty(inputEdit4.getText().toString())){
//			Toast.makeText(getApplicationContext(), "电话必须填写", Toast.LENGTH_SHORT).show();
//			return false;
		}else if(TextUtils.isEmpty(inputEdit5.getText().toString())){
			Toast.makeText(getApplicationContext(), "签证要求必须选择", Toast.LENGTH_SHORT).show();
			return false;
		}else if(TextUtils.isEmpty(inputEdit9.getText().toString())){
			Toast.makeText(getApplicationContext(), "薪资必须填写", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
	
	private void showChooseCountryDialog5(String title){
		View inputView = getLayoutInflater().inflate(R.layout.dialog_wheel_3_view, null);
		final WheelView wheel1 = (WheelView)inputView.findViewById(R.id.wheel_1);
		final WheelView wheel2 = (WheelView)inputView.findViewById(R.id.wheel_2);
		final WheelView wheel3 = (WheelView)inputView.findViewById(R.id.wheel_3);
		
		List<String> countryList = new ArrayList<String>();
		for(CountryData cd:BaseActivity.getCountryList(getApplicationContext())){
			countryList.add(cd.getN());
		}
        wheel1.setAdapter(new StringWheelAdapter(countryList));
        wheel1.setCurrentItem(nCountryIdx);
        wheel1.setCyclic(true);
        wheel1.setInterpolator(new AnticipateOvershootInterpolator());
        wheel1.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				nCountryIdx = newValue;
				nProvinceIdx = 0;
				nCityIdx = 0;
				
				List<String> provinceList = new ArrayList<String>();
				for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP()){
					provinceList.add(cd.getN());
				}
		        wheel2.setAdapter(new StringWheelAdapter(provinceList));
		        wheel2.setCurrentItem(nProvinceIdx);
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
					cityList.add(cd.getN());
				}
				wheel3.setAdapter(new StringWheelAdapter(cityList));
				wheel3.setCurrentItem(nCityIdx);
			}});
		
		List<String> provinceList = new ArrayList<String>();
		for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP()){
			provinceList.add(cd.getN());
		}
        wheel2.setAdapter(new StringWheelAdapter(provinceList));
        wheel2.setCurrentItem(nProvinceIdx);
        wheel2.setCyclic(true);
        wheel2.setInterpolator(new AnticipateOvershootInterpolator());
        wheel2.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				nProvinceIdx = newValue;
				nCityIdx = 0;
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
					cityList.add(cd.getN());
				}
				wheel3.setAdapter(new StringWheelAdapter(cityList));
				wheel3.setCurrentItem(nCityIdx);
			}});
        
		List<String> cityList = new ArrayList<String>();
		for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
			cityList.add(cd.getN());
		}
        wheel3.setAdapter(new StringWheelAdapter(cityList));
        wheel3.setCurrentItem(nCityIdx);
        wheel3.setCyclic(true);
        wheel3.setInterpolator(new AnticipateOvershootInterpolator());
        
		AlertDialog.Builder builder = new AlertDialog.Builder(CreateMutualTransaction3Activity.this);
		builder.setTitle(title);
		builder.setView(inputView);  
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				nCountryIdx = wheel1.getCurrentItem();
				nProvinceIdx = wheel2.getCurrentItem();
				nCityIdx = wheel3.getCurrentItem();
				if(TextUtils.isEmpty(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getN())){
					inputEdit3.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN());
				}else{
					inputEdit3.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN());
				}
			}
		});  
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){}
		}); 
		builder.show();  
	}

}
